<template>
    <base-breadcrumb>
        <searchData :searchData="searchArr" @search="search"></searchData>
        <a-card>
            <!-- <div class="table-operator">
                <a-button type="primary" icon="plus" @click="handleUpdate('add', {})" v-btnPermission="'bm_016_add_01'"
                    >新增</a-button
                >
            </div> -->
            <list-table
                ref="table"
                size="default"
                rowKey="key"
                :columns="columns"
                :request="loadData"
                :alert="true"
                show-size-changer
                show-quick-jumper
                showPagination="auto"
                :export-config="{
                        use: true,
                        select: '成本还原与总结',
                        all: '成本还原与总结'
                    }"
                :exportPermission="{ select: 'bm_016_exp_03', all: 'bm_016_exp_03' }"
                :statistics-config="{ use: true, total, temp }"
            >
                <span slot="description" slot-scope="text">
                    <ellipsis :length="8" tooltip>{{ text }}</ellipsis>
                </span>
                <span slot="operation" slot-scope="text, record">
                    <MoreButton>
                    <a v-btnPermission="'bm_016_loo_02'" @click="handleUpdate('look', record)">查看</a>
                    <span
                        v-btnPermission="'bm_016_edi_01'"
                        v-if="record.auditStatus != 'SUBMITTED' && record.auditStatus != 'PAST'&& record.auditStatus != 'ABOLISH'"
                    >
                        <a @click="handleUpdate('edit', record)">编辑</a>
                    </span>
                    <!-- <span v-btnPermission="'bm_016_del_03'" v-if="record.auditStatus == 'DRAFT'">
                        <a @click="handleUpdate('del', record, '删除')">删除</a>
                    </span>
                    <span v-btnPermission="'bm_016_tov_05'" v-if="record.auditStatus == 'PAST'">
                        <a @click="handleUpdate('invalid', record, '作废')">作废</a>
                    </span>
                    <span v-if="record.auditStatus == 'SUBMITTED' && userId === record.creatorId&&hideBuquBtn()">
                        <a @click="handleUpdate('undo', record, '撤销')">撤销</a>
                    </span> -->
                    <span
                        v-btnPermission="'bm_016_loo_02'"
                        v-if="
                            record.auditStatus === 'PAST' ||
                            record.auditStatus === 'SUBMITTED' ||
                            record.auditStatus === 'REJECTED'||
                            record.auditStatus === 'ABOLISH'
                        "
                    >
                        <approval-button :status="record.auditStatus" :id="record.id"></approval-button>
                    </span>
                        </MoreButton>
                </span>
                <span slot="auditStatus" slot-scope="text, record">
                    <list-status
                        :status="record.auditStatus"
                        :isApproval="true"
                        conversion="en"
                        :text="text"
                    ></list-status>
                </span>
            </list-table>
        </a-card>
    </base-breadcrumb>
</template>

<script>
import { getCostReduction, delCostReduction, cancelCostReduction, getCountList } from '@/api/businessAffairs/revenueAndCost'
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 项目
import OrgTreeSelect from '@/components/OrgTreeSelect'
const columns = [
    {
        title: '所属分公司',
        dataIndex: 'branchComName'
    },
    {
        title: '所属项目',
        dataIndex: 'projectName'
    },
    {
        title: '还原与总结完成时间',
        dataIndex: 'finishDate'
    },
    {
        title: '填报人',
        dataIndex: 'informant'
    },
    {
        title: '填报时间',
        dataIndex: 'informantDate'
    },
    {
        title: '状态',
        dataIndex: 'auditStatusName',
        // scopedSlots: { customRender: 'auditStatus' }
    },
    {
        title: '操作',
        dataIndex: 'operation',
        width: '230px',
        scopedSlots: { customRender: 'operation' }
    }
]
const searchArr = [
{
    name: '所属单位',
    valueKey: 'searchQuery',
    type: OrgTreeSelect,
  },
    // {
    //     name: '所属分公司',
    //     valueKey: 'branchComId',
    //     type: SelectSubCompany
    // },
    // {
    //     name: '所属项目',
    //     keyValue: 'branchComId',
    //     valueKey: 'projectId',
    //     type: SelectProject
    // },
    {
        name: '还原与总结完成时间：',
        value: 'finishDate',
        type: 'dateRange'
    },

    {
        name: '填报人',
        value: 'informant',
        type: 'input'
    },
    {
        name: '填报时间',
        value: 'startEndDate',
        type: 'dateRange'
    },
    {
        name: '状态',
        value: 'auditStatus',
        type: 'select',
        data: typeListApproval
    }
]
import { typeListApproval } from '@/utils/util'
import { STable } from '@/components'
export default {
    name: 'a' + Date.now(),
    components: {
        STable
    },
    data() {
        this.columns = columns
        this.searchArr = searchArr
        return {
            countList:{},
            total:{},
            queryParam: {},
            userId: JSON.parse(sessionStorage.getItem('ycloud-user_info')).id,
            temp: () => {
            return (
                <span style="margin-right: 12px">
                待提交：<span class="totalNum">{ this.countList.waitCommitCount }  </span> 个
                审批中：<span class="totalNum">{  this.countList.submittedCount }  </span>个
                审批通过：<span class="totalNum">{ this.countList.pastCount }</span>个
                审批不通过：<span class="totalNum">{ this.countList.rejectedCount }  </span>个  
                </span>
            )
          },
            loadData: async (parameter) => {
                const requestParameters = Object.assign({}, parameter, this.queryParam)
                this.getCountList(requestParameters)
                const [err, res] = await getCostReduction(requestParameters)
                return new Promise((resolve, reject) => {
                    if (err) {
                        reject(err)
                    } else {
                    res.data.forEach(item => {
                    if(item.auditStatus=='PAST'){
                        item.auditStatusName='审批通过'
                    }else if(item.auditStatus=='SUBMITTED'){
                        item.auditStatusName='审批中'
                    }else if(item.auditStatus=='REJECTED'){
                        item.auditStatusName='审批不通过'
                    }else if(item.auditStatus=='DRAFT'){
                        item.auditStatusName='待提交审批'
                    }
                        if(!item.informant) {
                            item.informantDate = ''
                        }
                });
                        resolve(res)

                    }
                })
            }
        }
    },
    methods: {
        getCountList(requestParameters){
            getCountList(requestParameters).then(res=>{
            if (res[1].code === 200) {
                this.countList=res[1].data
                // console.log(this.countList,'111111111111111')
              }
          })
        },
        //搜索
        search(val) {
            this.queryParam = {
                ...val,
                informantTimeBegin:
                    (val.startEndDate &&
                        val.startEndDate.length &&
                        val.startEndDate[0].format('YYYY-MM-DD HH:mm:ss')) ||
                    void 0,
                informantTimeEnd:
                    (val.startEndDate &&
                        val.startEndDate.length &&
                        val.startEndDate[1].format('YYYY-MM-DD HH:mm:ss')) ||
                    void 0,
                    finishBeginDate:
                    (val.finishDate &&
                        val.finishDate.length &&
                        val.finishDate[0].format('YYYY-MM-DD HH:mm:ss')) ||
                    void 0,
                    finishEndDate:
                    (val.finishDate &&
                        val.finishDate.length &&
                        val.finishDate[1].format('YYYY-MM-DD HH:mm:ss')) ||
                    void 0,
            }
            delete this.queryParam.startEndDate
            this.$refs.table.refresh(true)
        },
        async delCostReduction(data, type, text) {
            const [err, res] =
                type == 'del'
                    ? await delCostReduction({ id: data.id })
                    : await cancelCostReduction({ id: data.id, type: type == 'undo' ? 1 : 2 })

            if (!err) {
                this.$message.success(text + '成功')
                this.$refs.table.refresh(true)
            }
        },
        //新增
        async handleUpdate(type, data = {}, text = '') {
            if (type == 'del' || type == 'undo' || type == 'invalid') {
                let that = this
                this.$confirm({
                    title: '温馨提示',
                    content: '确定是否' + text + '数据？',
                    okText: '确认',
                    okType: 'danger',
                    cancelText: '取消',
                    onOk: () => {
                        return that.delCostReduction(data, type, text)
                    }
                })
            } else {
                this.$router.push({
                    name: 'updateCostReduction',
                    query: {
                        id: data.id || '',
                        pageType: type
                    }
                })
            }
        }
    }
}
</script>


